package com.evernote.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.evernote.db.DBUtil;
import com.evernote.entity.Content;

public class ContentDao extends BaseDao {
	
	/**
	 * 获取笔记本中所有笔记
	 * @param c
	 * @return
	 */
	public List<Content> findAllContentByNid(Content c) {
		List<Content> list = new ArrayList<>();
		try {
			connection = DBUtil.getConnection();
			String sql = "select * from content where Nid = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, c.getNid());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				Content ct = new Content();
				ct.setId(resultSet.getInt("id"));
				ct.setTitle(resultSet.getString("title"));
				ct.setDate(resultSet.getDate("date").toString());
				ct.setContent(resultSet.getString("content"));
				ct.setNid(resultSet.getInt("NId"));
				ct.setUid(resultSet.getInt("Uid"));
				list.add(ct);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return list;
	}
	
	/**
	 * 获取用户所有笔记
	 * @param c
	 * @return
	 */
	public List<Content> findAllContentByUid(Content c) {
		List<Content> list = new ArrayList<>();
		try {
			String sql = "select * from content where Uid = ?";
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, c.getUid());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				Content ct = new Content();
				ct.setId(resultSet.getInt("id"));
				ct.setTitle(resultSet.getString("title"));
				ct.setDate(resultSet.getDate("date").toString());
				ct.setContent(resultSet.getString("content"));
				ct.setNid(resultSet.getInt("NId"));
				ct.setUid(resultSet.getInt("Uid"));
				list.add(ct);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return list;
	}
	
	/**
	 * 获取用户笔记的数量
	 * @param c
	 * @return
	 */
	public int findAllContentNumByUid(Content c) {
		int count = 0;
		try {
			String sql = "select count(*) as count from content where Uid = ?";
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, c.getUid());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				count = resultSet.getInt("count");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return count;
	}
	
	/**
	 * 获取笔记本中笔记的数量
	 * @param c
	 * @return
	 */
	public int findAllNoteNumByNid(Content c) {
		int count = 0;
		try {
			String sql = "select count(*) as count from content where Nid = ? and Uid = ?";
			connection = DBUtil.getConnection();
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, c.getNid());
			preparedStatement.setInt(2, c.getUid());
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()) {
				count = resultSet.getInt("count");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return count;
	}
	
	/**
	 * 新增笔记并返回自增的id
	 * @param c
	 * @return 成功返回新增笔记的id 失败返回0
	 */
	public int addNote(Content c) {
		int count = 0;
		int Cid = 0;
		
		try {
			connection = DBUtil.getConnection();
			String sql = "insert into content(title, date, content, Nid, Uid) value(?,?,?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, c.getTitle());
			preparedStatement.setString(2, c.getDate());
			preparedStatement.setString(3, c.getContent());
			preparedStatement.setInt(4, c.getNid());
			preparedStatement.setInt(5, c.getUid());
			count = preparedStatement.executeUpdate();
			if(count != 0) {
				sql = "select last_insert_id() as Cid";
				preparedStatement = connection.prepareStatement(sql);
				resultSet = preparedStatement.executeQuery();
				while(resultSet.next()) {
					Cid = resultSet.getInt("Cid");
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return Cid;
	}
	
	/**
	 * 修改笔记
	 * @param c
	 * @return 成功返回1 失败0
	 */
	public int updateNoteByCid(Content c) {
		int count = 0;
		try {
			connection = DBUtil.getConnection();
			String sql = "update content set title = ?, date = ?, content = ?, Nid = ? where id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, c.getTitle());
			preparedStatement.setString(2, c.getDate());
			preparedStatement.setString(3, c.getContent());
			preparedStatement.setInt(4, c.getNid());
			preparedStatement.setInt(5, c.getId());
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return count;
	}
	
	/**
	 * 删除笔记
	 * @param n
	 * @return 失败返回0
	 */
	public int deleteNoteById(Content c) {
		int count = 0;
		try {
			connection = DBUtil.getConnection();
			String sql = "delete from content where id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, c.getId());
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return count;
	}
	
	/**
	 * 删除笔记本中所有笔记
	 * @param n
	 * @return 失败返回0
	 */
	public int deleteNoteByNid(Content c) {
		int count = 0;
		try {
			connection = DBUtil.getConnection();
			String sql = "delete from content where Nid = ? and Uid = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, c.getNid());
			preparedStatement.setInt(2, c.getUid());
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closed();
		}
		return count;
	}
	
}
